package cn.fl.institution.service.impl;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.capital.service.ICapPayInfoService;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.institution.api.IPrjInsRfeRecordApiService;
import cn.fl.institution.condition.PrjInsRfeRecordQC;
import cn.fl.institution.constant.PrjInsRfeRecordConstant;
import cn.fl.institution.dto.PrjInsRfeRecordDTO;
import cn.fl.institution.service.IPrjInsRfeRecordService;
import cn.fl.institution.transform.PrjInsRfeRecordVOTransform;
import cn.fl.institution.vo.PrjInsRfeRecordVO;
import cn.fl.project.constant.PrjBscInfoConstant;
import com.alibaba.fastjson.JSONObject;
import flow.business.facade.flow.api.IFlowBusinessQueryApiService;
import flow.business.facade.flow.condition.SysFlowSHQueryCondition;
import flow.business.facade.flow.dto.SysFlowSHDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Description: 资方补件记录表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class PrjInsRfeRecordServiceImpl implements IPrjInsRfeRecordService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(PrjInsRfeRecordServiceImpl.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IPrjInsRfeRecordApiService prjInsRfeRecordApiService;

	/**
	 * 字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	/**
	 *  流程同步接口
	 */
	@Autowired
	private IFlowBusinessQueryApiService flowBusinessQueryApiService;


	@Override
	public PrjInsRfeRecordVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//TODO 业务逻辑
		PrjInsRfeRecordDTO dto = this.prjInsRfeRecordApiService.selectById(id);
		return PrjInsRfeRecordVOTransform.toVO(dto);
	}

	@Override
	public Long add(PrjInsRfeRecordVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		return this.prjInsRfeRecordApiService.add(PrjInsRfeRecordVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.prjInsRfeRecordApiService.removeById(id);
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(PrjInsRfeRecordVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.prjInsRfeRecordApiService.modify(PrjInsRfeRecordVOTransform.toDTO(entity));
	}

	@Override
	public List<PrjInsRfeRecordVO> queryAll(PrjInsRfeRecordQC qc) throws BaseException {
		if(null == qc){
			BaseLogUtils.info(logger, "queryAll", "查询请求参数对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		List<PrjInsRfeRecordDTO> prjInsRfeRecordDTOS = this.prjInsRfeRecordApiService.selectList(qc);
		return PrjInsRfeRecordVOTransform.toVOList(prjInsRfeRecordDTOS);
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		return this.prjInsRfeRecordApiService.searchListPage(page);
	}

	@Override
	public Pager searchInsRfeRecordListPage(Pager page) throws BaseException {
		BaseLogUtils.newLogger( "查询新网补件记录展示列表Start:page={}", JSONObject.toJSONString(page)).info();
		Pager pager = this.prjInsRfeRecordApiService.searchInsRfeRecordListPage(page);
		if(CollectionUtils.isEmpty(pager.getResultList())){
			return pager;
		}
		List<PrjInsRfeRecordDTO> dtoList = (List<PrjInsRfeRecordDTO>) pager.getResultList();
		BaseLogUtils.newLogger( "查询新网补件记录展示列表End:page={},dtoList={}", JSONObject.toJSONString(page),JSONObject.toJSONString(dtoList)).info();
		if (dtoList != null && !dtoList.isEmpty()) {
			List<PrjInsRfeRecordVO> prjInsRfeRecordVOS = new ArrayList();
			List<PrjInsRfeRecordVO> voList = PrjInsRfeRecordVOTransform.toVOList(dtoList);
			for (PrjInsRfeRecordVO prjInsRfeRecordVO : voList) {

				//补件状态
				List<KeyValueVO> compltStsList = this.getCompltStsList();
				for (KeyValueVO keyValueVO : compltStsList) {
					if(StringUtils.isNotEmpty(prjInsRfeRecordVO.getCompltStsCd())) {
						if (Objects.equals(keyValueVO.getKey(), prjInsRfeRecordVO.getCompltStsCd())) {
							prjInsRfeRecordVO.setCompltStsCdNm(keyValueVO.getValue());
							break;
						}
					}
				}

				//补件节点
				List<KeyValueVO> compltNodeList = this.getCompltNodeList();
				for (KeyValueVO keyValueVO : compltNodeList) {
					if( StringUtils.isNotEmpty(prjInsRfeRecordVO.getCompltNodeCd())){

						if (Objects.equals(keyValueVO.getKey(), prjInsRfeRecordVO.getCompltNodeCd())) {
							prjInsRfeRecordVO.setCompltNodeCdNm(keyValueVO.getValue());
							break;
						}
					}
				}

				//付款状态
				List<KeyValueVO> payStsCdList = this.getPayStsCdList();
				for (KeyValueVO keyValueVO : payStsCdList) {
					if(StringUtils.isNotEmpty(prjInsRfeRecordVO.getPayerStsCd())) {
						if (Objects.equals(keyValueVO.getKey(), prjInsRfeRecordVO.getPayerStsCd())) {
							prjInsRfeRecordVO.setPayerStsCdNm(keyValueVO.getValue());
							break;
						}
					}
				}

				//2.根据业务id查询流程记录表
				SysFlowSHQueryCondition sysFlowSHQC = new SysFlowSHQueryCondition();
				sysFlowSHQC.setSysCd(CurrentThreadContext.getCurrentSysCd());//系统代码
				sysFlowSHQC.setWordsInTheTable(PrjBscInfoConstant.PRJ_FLOW_S_H_PRJ);//表中字
				sysFlowSHQC.setDataId(prjInsRfeRecordVO.getId());//业务主键
				sysFlowSHQC.setpDefKey(PrjInsRfeRecordConstant.FLOW_DEF_KEY_ADDITIONAL_DOCUMENT);
				sysFlowSHQC.setStatus(BaseConstant.IS_YESNO_YES);
				//查询流程记录表
				BaseLogUtils.newLogger( "新网补件查询补件流程状态Start:sysFlowSHQC={}", JSONObject.toJSONString(sysFlowSHQC)).info();
				List<SysFlowSHDTO> sysFlowSHDTOS = this.flowBusinessQueryApiService.searchFlowSHDTOList(sysFlowSHQC);
				BaseLogUtils.newLogger( "新网补件查询补件流程状态End:dataId={},sysFlowSHDTOS={}", prjInsRfeRecordVO.getId(),JSONObject.toJSONString(sysFlowSHDTOS)).info();
				if(CollectionUtils.isNotEmpty(sysFlowSHDTOS)){
					prjInsRfeRecordVO.setCompltFlowStsCd(sysFlowSHDTOS.get(0).getAudStsCd());
				}
				prjInsRfeRecordVOS.add(prjInsRfeRecordVO);
			}
			pager.setResultList(prjInsRfeRecordVOS);
		}
		return pager;
	}

	@Override
	public List<KeyValueVO> getCompltStsList() throws BaseException {
		String code = DictionaryConstant.CODE_COMPLT_STS_CD;
		return this.getKeyValueList(code);
	}

	@Override
	public List<KeyValueVO> getCompltNodeList() throws BaseException {
		String code = DictionaryConstant.CODE_COMPLT_NODE_CD;
		return this.getKeyValueList(code);
	}

	@Override
	public List<KeyValueVO> getPayStsCdList() throws BaseException {
		String code = DictionaryConstant.CODE_PAY_STS_CD;
		return this.getKeyValueList(code);
	}

	/**
	  * @Author yangzhongkun
	  * @params [code]
	  * @Description 查询字典项
	  * @Date 2020/6/23 16:06
	  * @return java.util.List<cmm.mid.core.framework.page.KeyValueVO>
	  **/
	private List<KeyValueVO> getKeyValueList(String code) throws BaseException {
		List<KeyValueVO> keyValueList = null;
		List<SysDictionaryDTO> dictionaryList = this.sysDictionaryApiService
				.searchGroup(CurrentThreadContext.getCurrentSysCd(),
						CurrentThreadContext.getCurrentOrgCd(), code);
		if (CollectionUtils.isNotEmpty(dictionaryList)) {
			keyValueList = new ArrayList<>();
			for (SysDictionaryDTO sysDictionaryDTO : dictionaryList) {
				KeyValueVO keyValue = new KeyValueVO();
				keyValue.setKey(sysDictionaryDTO.getCode());
				keyValue.setValue(sysDictionaryDTO.getCodeNm());
				keyValueList.add(keyValue);
			}
		}
		return keyValueList;
	}

}
